package edu.ccut.saturn.basevalidate.businessvalidate;

import java.util.Date;
import java.util.Map;

import edu.ccut.saturn.component.DateFormatManager;
import edu.ccut.saturn.component.DefaultSaturnValidate;
import edu.ccut.saturn.component.ValidateResult;

public class DateRangeValidate extends DefaultSaturnValidate {

	public void doValidate(Object value, Object[] args, ValidateResult result) {
		if (value != null && args != null && args.length >= 2) {
			Date orginDate = DateFormatManager.getDateTime(value.toString());
			Date floorDate = DateFormatManager.getDateTime(args[0].toString());
			Date topDate = DateFormatManager.getDateTime(args[1].toString());

			if (orginDate != null && floorDate != null && topDate != null) {
				if (orginDate.compareTo(floorDate) < 0
						|| orginDate.compareTo(topDate) > 0) {

					Map<String, String> errorInfos = getErrorInfos();
					String errorKey = "_error_";
					String errorInfo = null;

					if (errorInfos != null) {
						errorInfo = errorInfos.get(errorKey);
					}
					result.addValidateError(errorInfo, value);
				}
			}
		}
	}
}
